package com.ceo.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author uto_dev_008
 * @desc
 * @since 2021/11/24 10:11
 * Copyright (c)  All rights reserved
 */
@Configuration
public class CorsConfig {

    /* 跨域访问
     *  只允许GET、POST方法
     *  header新增自定义域authorization，用于携带tocken
     * */
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addExposedHeader("authorization");
        corsConfiguration.addExposedHeader("access_token");
        corsConfiguration.addExposedHeader("Content-disposition");
        return corsConfiguration;
    }

    //跨域访问
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}
